package com.socool.sinofaith.ui;

import java.util.ArrayList;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.deying.actionbarcompat.ActionBarHelperBase;
import com.deying.actionbarcompat.ActionBarHelperBase.CenterViewFactory;
import com.deying.actionbarcompat.ActionBarHelperBase.SpinnerViewFactory;
import com.socool.sinofaith.R;
import com.socool.sinofaith.SocoolApplication;
import com.socool.sinofaith.jsonparse.HttpJsonAdapter;
import com.socool.sinofaith.model.NoticeBean;
import com.socool.sinofaith.model.NoticeBean.Notice;
import com.socool.sinofaith.util.ConstansJsonUrl;
import com.socool.sinofaith.util.SinofaithUtil;

/**
 * 最新通知
 * 
 * @author maxhsh
 */
public class NoticeActivity extends BaseActivity implements CenterViewFactory,
		SpinnerViewFactory, View.OnClickListener {
	ActionBarHelperBase mActionBarHelperBase;
	private ListView mListView;
	NoticeAdapter mNoticeAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_nitice);
		SocoolApplication.addActivity(this);
		mActionBarHelperBase = (ActionBarHelperBase) this.getActionBarHelper();
		mActionBarHelperBase.setmCenterViewFactory(this);
		mActionBarHelperBase.setmSpinnerViewFactory(this);
		mActionBarHelperBase.setmClickListener(this);

		mListView = (ListView) findViewById(R.id.mListView);
		mNoticeAdapter = new NoticeAdapter(this);
		mListView.setAdapter(mNoticeAdapter);

		getData(getSPString());
	}

	NoticeBean baseNoticeBean;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	void getData(String _mobile) {
		FinalHttp fh = new FinalHttp();
		AjaxParams params = new AjaxParams();
		params.put("sid", ConstansJsonUrl.getAppPush);
		params.put("did", SinofaithUtil.did);
		params.put("mobile", _mobile);
		fh.get(ConstansJsonUrl.REQUEST_URL, params, new AjaxCallBack() {
			@Override
			public void onLoading(long count, long current) {
				// TODO Auto-generated method stub
				super.onLoading(count, current);
			}

			@Override
			public void onSuccess(Object t) {
				// TODO Auto-generated method stub
				super.onSuccess(t);
				try {
					baseNoticeBean = HttpJsonAdapter.getInstance().get(
							t.toString(), NoticeBean.class);
					if (baseNoticeBean.getResult().equals("0")) {
						mNoticeAdapter.setDatas(baseNoticeBean.getAppPushList());
					}
				} catch (Exception e) {
					Toast.makeText(NoticeActivity.this, e.getMessage(),
							Toast.LENGTH_LONG).show();
				}

			}

			@Override
			public void onFailure(Throwable t, int errorNo, String strMsg) {
				// TODO Auto-generated method stub
				super.onFailure(t, errorNo, strMsg);
				Toast.makeText(NoticeActivity.this, strMsg, Toast.LENGTH_LONG)
						.show();
			}

		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.common, menu);
		return super.onCreateOptionsMenu(menu);
	}

	public View createView() {

		TextView tv = new TextView(this);
		tv.setText("最新通知");
		tv.setTextColor(this.getResources().getColor(R.color.white));
		tv.setTextSize(20);
		return tv;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated
															// method stub
		if (item.getItemId() == R.id.home) {
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public View createSpinnerView() {
		ImageView imageview = new ImageView(this);
		return imageview;
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btnPrevious:
			break;
		}
	}

	class NoticeAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private Activity mActivity;
		ArrayList<Notice> datas;

		public ArrayList<Notice> getDatas() {
			return datas;
		}

		public void setDatas(ArrayList<Notice> datas) {
			this.datas = datas;
			this.notifyDataSetChanged();
		}

		public NoticeAdapter(Activity _activity) {
			super();
			this.mActivity = _activity;
			mInflater = LayoutInflater.from(_activity);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (datas != null) {
				return datas.size();
			} else {
				return 0;
			}
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			arg1 = arg1 != null ? arg1 : mInflater.inflate(
					R.layout.notice_item, null, false);
			TextView messageContent = (TextView) arg1
					.findViewById(R.id.messageContent);
			TextView messageTime = (TextView) arg1
					.findViewById(R.id.messageTime);
			final Notice mNotice = datas.get(arg0);
			messageContent.setText(Html.fromHtml(mNotice.getTitle()));
			messageTime.setText(mNotice.getSendTime());
			arg1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent mIntent = new Intent();
					mIntent.putExtra("mNotice", mNotice);
					mIntent.setClass(mActivity, NoticeDetailActivity.class);
					mActivity.startActivity(mIntent);

				}
			});

			return arg1;
		}

	}
	private String getSPString(){
		SharedPreferences sp=this.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}

}
